import 'package:get/get.dart';
import 'package:wk_robot2/app/robot_comm/mqtt_comm_manager.dart';
import 'package:wk_robot2/app/storage/StorageManager.dart';
import 'package:wk_robot2/app/utils/CommonUtil.dart';

import '../../../const/AppConst.dart';
import '../../../event/DataCallBack.dart';
import '../../../nets/NetWorkManager.dart';
import '../../../widget/tips.dart';
import '../../componentview/controllers/IndexViewController.dart';

class RobotsDetailController extends GetxController {

  var robotUserId = ''.obs;
  var robotWhetherOnline = false.obs;

  @override
  void onInit() {
    super.onInit();
    robotUserId.value = Get.parameters['robotUserId']!;

    robotWhetherOnline.value = CommonUtil.getCurrentSelectedRobot().isOnline ?? false;
  }

  @override
  void onReady() {
    super.onReady();

  }

  @override
  void onClose() {
    super.onClose();
  }


  void unbind() {
    NetWorkManager.getInstance().requestBussiness(NetWorkManager.BIND_CONFIG_ROBOTE_UNBIND, {
      'robotUserId': robotUserId.value
    }, NetWorkManager.methodPOST, DataCallBack(
        businessCallBack: (v) {
          StorageManager.getStorage().remove(AppConst.BLT_DEVICE_SN_CURRENTLY);
          MqttCommManager().unsubscribeMsg(robotUserId.value);

          IndexViewController indexViewController = GetInstance().find<IndexViewController>();
          if (indexViewController != null) {
            indexViewController.retrieveRobots();
          }

          Get.back();
          Tips.showToast('解绑成功');
        }
    ));
  }
}
